const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function deepClone(initalObj, finalObj) {
  var obj = finalObj || {};
  for (var i in initalObj) {
    var prop = initalObj[i];
    // 避免相互引用对象导致死循环，如initalObj.a = initalObj的情况
    if (prop === obj) {
      continue;
    }
    if (typeof prop === 'object') {
      obj[i] = (prop.constructor === Array) ? [] : {};
      arguments.callee(prop, obj[i]);
    } else {
      obj[i] = prop;
    }
  }
  return obj;
}
// 首尾去空
function trim (str = ''){
  return str.replace(/^\s+|\s$/g, '')
}

// 18位数身份证正则
function REGEXP_IDCARD18(id_card){
  return /^[1-9][0-7]\d{4}((\d{4}(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(\d{4}(0[13578]|1[02])31)|(\d{4}02(0[1-9]|1\d|2[0-8]))|(\d{2}([13579][26]|[2468][048]|0[48])0229))\d{3}(\d|X|x)$/.test(id_card)
}
// 验证手机 

function REGEXP_MOBILEPHONE(phone){
  return /^0?(1[3456789][0-9])[0-9]{8}$/.test(phone)
}


module.exports = {
  formatTime: formatTime,
  deepClone: deepClone,
  trim: trim,
  reg_idCard: REGEXP_IDCARD18,
  reg_phone: REGEXP_MOBILEPHONE
}
